package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 套餐 持久层接口
 */
public interface SetmealDao {

    /**
     * 根据条件分页查询
     *
     * @param queryString 查询条件
     * @return 实体类 查询结果页面,查询结果总条数
     */
    @Select("<script> select * from t_setmeal <if test=\" queryString !=null and queryString !='' \">where code like \"%\"#{queryString}\"%\" or name like \"%\"#{queryString}\"%\" or helpCode like \"%\"#{queryString}\"%\" </if> </script>")
    Page<Setmeal> selectByCondition(@Param("queryString") String queryString);

    /**
     * 添加套餐  查询添加的主键
     *
     * @param setmeal 套餐
     */
    @Insert("insert into t_setmeal values(null,#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @SelectKey(statement = "select last_insert_id() ", keyProperty = "id", before = false, resultType = Integer.class, keyColumn = "id")
    void add(Setmeal setmeal);

    /**
     * 添加套餐-检查组中间表
     *
     * @param map 套餐id  检查组id
     */
    @Insert("insert into t_setmeal_checkgroup values(#{setmealId},#{checkgroupId})")
    void setSetmealAndCheckGroup(Map<String, Integer> map);

    /**
     * 查询所有套餐图片名称
     *
     * @return List<String> 图片名称集合
     */
    @Select("select img from t_setmeal ")
    List<String> findAllImg();

    /**
     * 通过id查询套餐  关联查询相应检查组
     *
     * @param setmealId 套餐id
     * @return 套餐
     */
    @Select("select * from t_setmeal where id=#{setmealId} ")
    @Results({
            @Result(property = "id", column = "id"),
            //1对多的延迟加载
            @Result(property = "checkGroups", column = "id", many = @Many(select = "com.itheima.dao.CheckGroupDao.findCheckGroupsBySetmealId")),
    })
    Setmeal findById(Integer setmealId);

    /**
     * 根据套餐id,查询关联的检查组id
     *
     * @param setmealId 套餐id
     * @return List<Integer>  检查组id集合
     */
    @Select("select  checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{setmealId}")
    List<Integer> findCheckGroupIdBysetMealId(Integer setmealId);

    /**
     * 根据套餐id删除套餐-检查组中间表
     *
     * @param setmealId 套餐id
     */
    @Delete("delete  from t_setmeal_checkgroup where setmeal_id=#{setmealId}")
    void deleteAssociation(Integer setmealId);

    /**
     * 编辑套餐
     *
     * @param setmeal 套餐
     */
    @Update("update t_setmeal set name=#{name},code=#{code},helpCode=#{helpCode},sex=#{sex},age=#{age},price=#{price},remark=#{remark},attention=#{attention},img=#{img} where id=#{id} ")
    void edit(Setmeal setmeal);

    /**
     * 根据套餐id删除套餐
     *
     * @param setmealId 套餐id
     */
    @Delete("delete from t_setmeal where id=#{setmealId} ")
    void delete(Integer setmealId);

    /**
     * 查询所有套餐
     *
     * @return List<Setmeal>套餐集合
     */
    @Select("select * from t_setmeal ")
    List<Setmeal> findAll();

    /**
     * 根据套餐id查询套餐基本详情
     *
     * @param id 套餐id
     * @return 套餐
     */
    @Select("select * from t_setmeal where id=#{id}")
    Setmeal findSetmealBasicById(Integer id);

    /**
     * 查询套餐名称 使用次数
     *
     * @return 套餐名称 使用次数
     */
    @Select("select s.name  ,count(o.id) value from t_order o ,t_setmeal s where o.setmeal_id=s.id group by s.name")
    List<Map<String, Object>> findSetmealCount();


}
